package com.example.week04_work2;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author nhsoft.ll
 */
public class FutureTaskDemo {
    public static void main(String[] args) throws Exception{
        long start = System.currentTimeMillis();
        FutureTaskSumTask sumTask = new FutureTaskSumTask();
        FutureTask<Integer> futureTask = new FutureTask<>(sumTask);
        new Thread(futureTask).start();
        System.out.println("异步计算结果为：" + futureTask.get());
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
    }
}

class FutureTaskSumTask implements Callable<Integer> {

    private int result;

    public int getResult() {
        return result;
    }

    @Override
    public Integer call() throws Exception {
        result = sum();
        System.out.println("线程运行结果:" + result);
        return result;
    }

    private int sum() {
        return fibo(36);
    }

    private int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}

